package com.example.jyjava.controller;

import com.alibaba.fastjson.JSON;
import com.example.jyjava.bean.*;
import com.example.jyjava.dao.CollectDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private CollectDao collectDao;

    @RequestMapping("/lookCollect")
    public String lookCollect(@RequestBody Collect collect){
        String falg = "400";
        int i = collectDao.getCollectCounts(collect.getUserId(),collect.getShopId(),collect.getType());
        HashMap<String, Object> res = new HashMap<>();
        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("collectNum",i);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/addCollect")
    public String addCollect(@RequestBody Collect collect){
        String falg = "400";
        collect.setDeleteType(false);
        collect.setCreatTime(new Date());
        collect.setUpdateTime(new Date());
        collect.setDeleteType(false);
        int i = collectDao.addCollocted(collect);
        HashMap<String, Object> res = new HashMap<>();
        if (i > 0){
            falg = "200";
            res.put("flag",falg);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/getAllCollect")
    public String getAllCollect(@RequestBody Collect collect){
        String falg = "400";
        List<Collect> collectList = collectDao.getAllCollect(collect.getUserId());
        HashMap<String, Object> res = new HashMap<>();
        if (collectList != null){
            falg = "200";
            res.put("flag",falg);
            res.put("collectList",collectList);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/deleteDeleteType")
    public String deleteDeleteType(@RequestBody Collect collect){
        String falg = "400";
        int i = collectDao.deleteDeleteType(collect.getId());
        HashMap<String, Object> res = new HashMap<>();
        if (i > 0){
            falg = "200";
            res.put("flag",falg);
            res.put("collectList",i);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }


    @RequestMapping("/getCollect")
    public String getCollect(ShopQueryInfo shopQueryInfo){
        String falg = "400";
        int numbers = collectDao.getCollectC("%" + shopQueryInfo.getQuery() + "%",shopQueryInfo.getUserId());
        int pageStart = (shopQueryInfo.getPageNum()-1)*shopQueryInfo.getPageSize();
        List<Collect> collectList = collectDao.getCollect("%" + shopQueryInfo.getQuery() + "%", pageStart, shopQueryInfo.getPageSize(),shopQueryInfo.getUserId());
        HashMap<String, Object> res = new HashMap<>();
        if (collectList != null){
            falg = "200";
            res.put("flag",falg);
            res.put("collectList",collectList);
            res.put("number",numbers);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/getCollectId")
    public String getCollectId(@RequestBody Collect collect){
        String falg = "400";
        Collect co = collectDao.getCollectId(collect.getId());
        HashMap<String, Object> res = new HashMap<>();
        if (co != null){
            falg = "200";
            res.put("flag",falg);
            res.put("collectList",co);
        }else{
            res.put("flag",falg);
        }
        String s = JSON.toJSONString(res);
        return s;
    }
}
